package com.nitty.cash.loop.utils.helper

import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import org.json.JSONArray
import org.json.JSONObject
import kotlin.random.Random

/**
 * ConfigsHelper
 * @Author wwdeng
 * @CreateTime: 2024/11/28 12:04
 * @Annotation：自定义数据 帮助类
 */
object NittyConfigsHelper {

    /**
     * 自定义数据来源
     *     1：客服配置
     *
     * "customData": {
     *         "privacyPolicyLink": "https://www.facebook.com",
     *         "loginUpTime": "8 minutes",
     *         "defaultAmount": "20000",
     *         "workDate": "Monday to Sunday",
     *         "workTime": "9:00am to 6:30pm"
     *     },
     *
     */

    /**
     * 获取客服配置
     */
    fun getNittyConfigData(): JSONObject? {
        val dataString = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun getNittyCustomData(): JSONObject?{
        val customize = getNittyConfigData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 获取启动权限
     */
    fun getNittyPermissionsLaunch(): ArrayList<String>{
        val permissionLaunchList = arrayListOf<String>()
        val customize = getNittyConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                val array = permissions.optJSONArray("permission")
                if (array != null) {
                    for (i in 0..<array.length()){
                        permissionLaunchList.add(array.optString(i))
                    }
                }
            }
        }
        return permissionLaunchList
    }

    /**
     * 获取首页权限
     */
    fun getNittyPermissionsHome(): ArrayList<String>{
        val permissionHomeList = arrayListOf<String>()
        val customize = getNittyConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                val array = permissions.optJSONArray("home")
                if (array != null) {
                    for (i in 0..<array.length()){
                        permissionHomeList.add(array.optString(i))
                    }
                }
            }
        }
        return permissionHomeList
    }

    /**
     * 获取所有权限列表 = 启动权限 + 首页权限
     */
    fun getNittyPermissionsList(): ArrayList<String>{
        val permissionList = arrayListOf<String>()
        //启动权限
        val permissionLaunch = getNittyPermissionsLaunch()
        if (permissionLaunch.size > 0){
            permissionList.addAll(permissionLaunch)
        }

        //首页权限
        val permissionHome = getNittyPermissionsHome()
        if (permissionHome.size > 0){
            for (i in 0..<permissionHome.size){
                val str = permissionHome[i]
                if (permissionList.contains(str)){
                    continue
                }
                permissionList.add(str)
            }
        }
        return permissionList
    }

    /**
     * 获取隐私协议链接
     */
    fun getNittyPrivacyPolicyLink(): String{
        val customData = getNittyCustomData() ?: return ""
        return customData.optString("privacyPolicyLink","")
    }

    /**
     * 获取用户使用协议
     */
    fun getNittyTermsUseLink(): String{
        val customData = getNittyCustomData() ?: return ""
        if (customData.has("termsUseLink")){
            return customData.optString("termsUseLink","")
        }
        return ""
    }

    /**
     * 获取牌照信息
     */
    fun getNittyLicenseInfo(): JSONObject?{
        val customData = getNittyCustomData() ?: return null
        var licenseData: JSONObject? = null
        if (customData.has("licenseInfo")){
            licenseData = customData.optJSONObject("licenseInfo")
        }

        //是否是 斯瓦希里语
//        val curtLocale = Languages.getAppLanguage(MyApps.app.getAppContext())
//        if (customData.has("licenseInfo_sw") && curtLocale.language == LocaleContract.getSwahiliLocale().language){
//            licenseData = customData.optJSONObject("licenseInfo_sw")
//        }
        return licenseData
    }

    /**
     * 是否显示牌照信息
     */
    fun isNittyShowLicense(): Boolean{
        val licenseInfo = getNittyLicenseInfo() ?: return false
        return licenseInfo.optString("isShow") == "YES"
    }

    /**
     * 审核中、放款中是否显示公告信息
     */
    fun isNittyAuditNotice(): Boolean{
        val customData = getNittyCustomData() ?: return false
        if (customData.has("noticeInfo")){
            val noticeInfo = customData.optJSONObject("noticeInfo") ?: return false
            return noticeInfo.optString("isShow","NO") == "YES"
        }
        return false
    }

    /**
     * 获取 审核中、放款中 显示的公告信息
     */
    fun getNittyAuditNoticeInfo(): JSONObject?{
        val customData = getNittyCustomData() ?: return null

        var noticeData: JSONObject? = null
        if (customData.has("noticeInfo")){
            //默认为英语
            noticeData = customData.optJSONObject("noticeInfo")
        }

        //是否是 斯瓦希里语
//        val curtLocale = Languages.getAppLanguage(MyApps.app.getAppContext())
//        if (customData.has("noticeInfo_sw") && curtLocale.language == LocaleContract.getSwahiliLocale().language){
//            noticeData = customData.optJSONObject("noticeInfo_sw")
//        }
        return noticeData
    }

    /**
     * SMS Content 关键字过滤
     */
    fun getNittySmsContentKeys(): MutableSet<String>?{
        val customData = getNittyCustomData() ?: return null
        if (customData.has("smsContentKeys")){
            val smsContentKeys = customData.optJSONArray("smsContentKeys")
            if (smsContentKeys != null) {
                val keysSet = mutableSetOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysSet.add(smsContentKeys.optString(i))
                }
                return keysSet
            }
        }
        return null
    }


    /**
     * SMS Mobile 关键字过滤
     */
    fun getNittySmsMobileKeys(): MutableSet<String>?{
        val customData = getNittyCustomData() ?: return null
        if (customData.has("smsMobileKeys")){
            val smsContentKeys = customData.optJSONArray("smsMobileKeys")
            if (smsContentKeys != null) {
                val keysSet = mutableSetOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysSet.add(smsContentKeys.optString(i))
                }
                return keysSet
            }
        }
        return null
    }

    /**
     * 获取登录等待时长
     */
    fun getNittyLoginUpTime(): String{
        var loginUpTime = "8 minutes"
        val customData = getNittyCustomData()
        if (customData != null) {
            loginUpTime = customData.optString("loginUpTime")
        }
        return loginUpTime
    }

    /**
     * 获取默认额度
     */
    fun getNittyDefaultAmount(): String{
        var defaultAmount = "20000"
        val customData = getNittyCustomData()
        if (customData != null) {
            defaultAmount = customData.optString("defaultAmount")
        }
        return defaultAmount
    }

}